/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.src.uniandes.ecos.tsp.creditscore.algorithm;

import com.src.uniandes.ecos.tsp.creditscore.base.GeneralConstants;
import com.src.uniandes.ecos.tsp.creditscore.entity.ClienteProducto;
import com.src.uniandes.ecos.tsp.creditscore.enums.LongCreditHistoryEnum;
import com.src.uniandes.ecos.tsp.creditscore.exception.CreditScoreException;
import com.src.uniandes.ecos.tsp.creditscore.helper.CreditScoreHelper;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author AndhyMao
 */
public class RulesLongCreditHistory {

    public double getPointsProductUsageTime(Integer cliCodigo) throws CreditScoreException {
        double points = 0;
        double sumPoints = 0;
        List<ClienteProducto> listClientesProducto = null;
        Calendar calendar = Calendar.getInstance();
        Date today = new Date();
        Date dateRangeOne = null;
        Date dateRangeTwo = null;
        BigDecimal rangeOne;
        BigDecimal rangeTwo;
        BigDecimal rangeThree;
        int compareTo = 0;
        try {
            listClientesProducto = CreditScoreHelper.getInstance().findClienteProductoEntitiesByCliCodigoAndTipCodigo(cliCodigo, GeneralConstants.getInstance().CREDIT_CART_TIP_CODIGO);
            if (listClientesProducto != null && !listClientesProducto.isEmpty()) {
                for (ClienteProducto clienteProducto : listClientesProducto) {
                    /**
                     * calculating ranges
                     */
                    rangeOne = clienteProducto.getClpCupdis().multiply(new BigDecimal("0.25"));
                    rangeTwo = clienteProducto.getClpCupdis().multiply(new BigDecimal("0.50"));
                    rangeThree = clienteProducto.getClpCupdis().multiply(new BigDecimal("0.75"));
                    /**
                     * before 24 months
                     */
                    calendar.add(Calendar.MONTH, -24);
                    dateRangeOne = calendar.getTime();
                    if (clienteProducto.getClpFecape().before(dateRangeOne)) {
                        compareTo = clienteProducto.getClpCuputi().compareTo(rangeOne);
                        if (compareTo == -1 || compareTo == 0) {
                            sumPoints += LongCreditHistoryEnum.RANGE_ONE_LESS_THAN_25_PERCENTAGE.getPoints();
                        }
                        else {
                            compareTo = clienteProducto.getClpCuputi().compareTo(rangeTwo);
                            if (compareTo == -1 || compareTo == 0 && clienteProducto.getClpCuputi().compareTo(rangeOne) == 1) {
                                sumPoints += LongCreditHistoryEnum.RANGE_ONE_LESS_THAN_50_PERCENTAGE.getPoints();
                            }
                            else {
                                compareTo = clienteProducto.getClpCuputi().compareTo(rangeThree);
                                if (compareTo == -1 || compareTo == 0 && clienteProducto.getClpCuputi().compareTo(rangeTwo) == 1) {
                                    sumPoints += LongCreditHistoryEnum.RANGE_ONE_LESS_THAN_75_PERCENTAGE.getPoints();
                                }
                                else {
                                    sumPoints += LongCreditHistoryEnum.RANGE_ONE_GREATER_THAN_75_PERCENTAGE.getPoints();
                                }
                            }
                        }
                    }
                    /**
                     * after, equals 24 months adn before 12 months
                     */
                    calendar.setTime(today);
                    calendar.add(Calendar.MONTH, -12);
                    dateRangeTwo = calendar.getTime();
                    if ((clienteProducto.getClpFecape().after(dateRangeOne) || clienteProducto.getClpFecape().equals(dateRangeOne)) && clienteProducto.getClpFecape().before(dateRangeTwo)) {
                        compareTo = clienteProducto.getClpCuputi().compareTo(rangeOne);
                        if (compareTo == -1 || compareTo == 0) {
                            sumPoints += LongCreditHistoryEnum.RANGE_TWO_LESS_THAN_25_PERCENTAGE.getPoints();
                        }
                        else {
                            compareTo = clienteProducto.getClpCuputi().compareTo(rangeTwo);
                            if (compareTo == -1 || compareTo == 0 && clienteProducto.getClpCuputi().compareTo(rangeOne) == 1) {
                                sumPoints += LongCreditHistoryEnum.RANGE_TWO_LESS_THAN_50_PERCENTAGE.getPoints();
                            }
                            else {
                                compareTo = clienteProducto.getClpCuputi().compareTo(rangeThree);
                                if (compareTo == -1 || compareTo == 0 && clienteProducto.getClpCuputi().compareTo(rangeTwo) == 1) {
                                    sumPoints += LongCreditHistoryEnum.RANGE_TWO_LESS_THAN_75_PERCENTAGE.getPoints();
                                }
                                else {
                                    sumPoints += LongCreditHistoryEnum.RANGE_TWO_GREATER_THAN_75_PERCENTAGE.getPoints();;
                                }
                            }
                        }
                    }
                    /**
                     * after, equals 12 months adn before today
                     */
                    if (clienteProducto.getClpFecape().after(dateRangeTwo) || clienteProducto.getClpFecape().equals(dateRangeTwo)) {
                        compareTo = clienteProducto.getClpCuputi().compareTo(rangeOne);
                        if (compareTo == -1 || compareTo == 0) {
                            sumPoints += LongCreditHistoryEnum.RANGE_TREE_LESS_THAN_25_PERCENTAGE.getPoints();
                        }
                        else {
                            compareTo = clienteProducto.getClpCuputi().compareTo(rangeTwo);
                            if (compareTo == -1 || compareTo == 0 && clienteProducto.getClpCuputi().compareTo(rangeOne) == 1) {
                                sumPoints += LongCreditHistoryEnum.RANGE_TREE_LESS_THAN_50_PERCENTAGE.getPoints();
                            }
                            else {
                                compareTo = clienteProducto.getClpCuputi().compareTo(rangeThree);
                                if (compareTo == -1 || compareTo == 0 && clienteProducto.getClpCuputi().compareTo(rangeTwo) == 1) {
                                    sumPoints += LongCreditHistoryEnum.RANGE_TREE_LESS_THAN_75_PERCENTAGE.getPoints();
                                }
                                else {
                                    sumPoints += LongCreditHistoryEnum.RANGE_TREE_GREATER_THAN_75_PERCENTAGE.getPoints();
                                }
                            }
                        }
                    }
                }
                points = sumPoints / listClientesProducto.size();
            }
        }
        catch (Exception e) {
            throw new CreditScoreException("RulesLongCreditHistory:getPointsProductUsageTime:" + e.toString(), e);
        }
        return points;
    }
}
